package pr.vodafone.dao;


import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;
import pr.vodafone.dao.dto.Factura;
import java.io.Reader;
import java.util.List;

public class FacturaDAO {

	private SqlSessionFactory sqlSessionFactory;
    
	private SqlSessionFactory getSqlSessionFactory()
	{
		if (sqlSessionFactory == null){
			try {				
				String resource = "conf/mybatis-config.xml";
				Reader reader = Resources.getResourceAsReader(resource);
				sqlSessionFactory = new SqlSessionFactoryBuilder().build(reader);
			}catch (Exception e){
				e.printStackTrace();
			}
		}
		return sqlSessionFactory;
	}

	public Factura getFactura(int id)
	{			
		Factura fac = null;
		SqlSession session = getSqlSessionFactory().openSession();
		try{
			fac = session.selectOne("getFactura", id);
		}finally{
			session.close();
		}
		return fac;
	}
		
	public List<Factura> getFacturasByLinea(String telf) throws Exception
	{
		List<Factura> facturas = null;
		SqlSession session = getSqlSessionFactory().openSession();
		try{
			facturas = session.selectList("getFacturasByLinea", telf);
			
		}finally{
			session.close();
		}
		return facturas;
	}



	
}
